/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.myneed

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import kotlinx.android.synthetic.main.fragment_my_need.*
import top.stime21.myneighbours.R
import top.stime21.myneighbours.base.BuzMvpFragment
import top.stime21.myneighbours.base.setOnLoadMore
import top.stime21.myneighbours.data.entity.Need
import top.stime21.myneighbours.module.home.NeedAdapter
import top.stime21.myneighbours.module.login.NeedContract
import top.stime21.myneighbours.module.login.presenter.NeedPrensenterImpl

@SuppressLint("ValidFragment")
/**
 * Author: czmc  Email: @tuwan21.com
 * Version:	1.0  Date:3/5/2018
 */

class MyNeedFragment : BuzMvpFragment<NeedContract.Presenter>, NeedContract.View {
    override fun onDetailsGetSuccess(data: Need?) {
    }
    override fun onFinishSuccess() {

    }
    override fun onMyNeedGetSuccess(data: List<Need>?) {
        (recycleView.adapter as NeedAdapter).setUpdateData(data)
    }
    override fun onMyNeedMoreGetSuccess(data: List<Need>?) {
        var data =  (recycleView.adapter as NeedAdapter).data!!.plus(data!!)
        (recycleView.adapter as NeedAdapter).setUpdateData(data)
    }


    override fun onPublishSuccess() {
    }
    override fun onAcceptSuccess() {
    }

    override fun onCancleSuccess() {
    }

    override fun onNearbySuccess(data: List<Need>?) {
    }

    override fun onSearchSuccess(data: List<Need>?) {
    }
    override fun getLayoutId(): Int {
        return R.layout.fragment_my_need
    }
    override fun getPresenter(): NeedContract.Presenter {
        return NeedPrensenterImpl(this)
    }
    private var status: Int? = 0

    constructor(status: Int) {
        var ar: Bundle = Bundle()
        ar.putInt("status", status)
        arguments = ar
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        vLoading!!.setRetryListener {  mPrensenter!!.myneed(status!!) }
        if (arguments != null)
            status = arguments!!.getInt("status")
        recycleView!!.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        recycleView.adapter = NeedAdapter(context())
        mPrensenter!!.myneed(status!!)
        refreshView.setOnRefreshListener { mPrensenter!!.myneed(status!!) }
        recycleView.setOnLoadMore { mPrensenter!!.myneedMore(status!!) }
    }


    override fun showViewLoadding() {
        vLoading!!.showLoading()
    }

    override fun showEmpty() {
        vLoading!!.showEmpty()
        refreshView.isRefreshing = false
    }

    override fun showContent() {
        vLoading!!.showContent()
        refreshView.isRefreshing = false
    }

    override fun showError() {
        vLoading!!.showError()
        refreshView.isRefreshing = false
    }

}